package com.test.controller;

import com.test.bean.ReStudent;
import com.test.bean.Result;
import com.test.bean.Student;
import com.test.service.UserService;
import com.test.utils.ImportExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("reward")

public class RewardController {
    @Autowired
    private UserService userService;

//    学生奖助信息单条录入
    @ResponseBody
    @RequestMapping(value = "add",method = RequestMethod.POST)
    public Result AddReward(ReStudent restudent, HttpSession session, HttpServletResponse response){
        Result result = new Result();
        try {
            userService.addReStudent(restudent);
            result.setMessage("录入成功");
            result.setCode(200);
        }catch (Exception e){
            e.printStackTrace();
            result.setMessage("录入失败");
            result.setCode(400);//异常
        }
        return result;
    }

//    学生奖助信息excel录入
    @ResponseBody
    @RequestMapping(value = "adds",method = RequestMethod.POST)
    public class LeadingExcelController {

        /**
         * 描述：通过传统方式form表单提交方式导入excel文件
         *
         * @param request
         * @throws Exception
         */
        @RequestMapping("/form")
        public String form(HttpServletRequest request) throws Exception {
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

            InputStream in = null;
            List<List<Object>> listob = null;
            MultipartFile file = multipartRequest.getFile("upfile");

            if (file.isEmpty()) {
                throw new Exception("文件不存在！");
            }
            in = file.getInputStream();
            listob = new ImportExcelUtil().getBankListByExcel(in, file.getOriginalFilename());
            in.close();

            //该处可调用service相应方法进行数据保存到数据库中，现只对数据输出
            for (int i = 0; i < listob.size(); i++) {
                List<Object> lo = listob.get(i);
                ReStudent restudent = new ReStudent();
                restudent.setId(Integer.parseInt(String.valueOf(lo.get(0))));
                restudent.setName(String.valueOf(lo.get(1)));
                restudent.setContent(String.valueOf(lo.get(2)));
                restudent.setMoney(Integer.parseInt(String.valueOf(lo.get(3))));
                restudent.setRank(String.valueOf(lo.get(4)));
                restudent.setTeam_personal(Integer.parseInt(String.valueOf(lo.get(5))));
                restudent.setDuty_number(Integer.parseInt(String.valueOf(lo.get(6))));
                restudent.setReward_class(String.valueOf(lo.get(7)));
                restudent.setDate(String.valueOf(lo.get(8)));
                restudent.setUnit(String.valueOf(lo.get(9)));
                restudent.setGrant_type(Integer.parseInt(String.valueOf(lo.get(10))));
                restudent.setRemark(String.valueOf(lo.get(11)));

            }

            return null;
        }
    /**
     * 描述：通过 jquery.form.js 插件提供的ajax方式上传文件
     * @param request
     * @param response
     * @throws Exception
     */
        @ResponseBody
        @RequestMapping(value="ajaxUpload.do",method={RequestMethod.GET,RequestMethod.POST})
        public  Result  ajaxUploadExcel(HttpServletRequest request,HttpServletResponse response) throws Exception {
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;

            Result result = new Result();
    //            System.out.println("通过 jquery.form.js 提供的ajax方式上传文件！");

            InputStream in =null;
            List<List<Object>> listob = null;
            MultipartFile file = multipartRequest.getFile("upfile");
            if(file.isEmpty()){
                result.setCode(400);
                result.setMessage("excel解析录入失败");
            }

            in = file.getInputStream();
            listob = new ImportExcelUtil().getBankListByExcel(in,file.getOriginalFilename());

            //该处可调用service相应方法进行数据保存到数据库中，现只对数据输出
            for (int i = 0; i < listob.size(); i++) {
                List<Object> lo = listob.get(i);
                ReStudent restudent = new ReStudent();
                restudent.setId(Integer.parseInt(String.valueOf(lo.get(0))));
                restudent.setName(String.valueOf(lo.get(1)));
                restudent.setContent(String.valueOf(lo.get(2)));
                restudent.setMoney(Integer.parseInt(String.valueOf(lo.get(3))));
                restudent.setRank(String.valueOf(lo.get(4)));
                restudent.setTeam_personal(Integer.parseInt(String.valueOf(lo.get(5))));
                restudent.setDuty_number(Integer.parseInt(String.valueOf(lo.get(6))));
                restudent.setReward_class(String.valueOf(lo.get(7)));
                restudent.setDate(String.valueOf(lo.get(8)));
                restudent.setUnit(String.valueOf(lo.get(9)));
                restudent.setGrant_type(Integer.parseInt(String.valueOf(lo.get(10))));
                restudent.setRemark(String.valueOf(lo.get(11)));

    //          System.out.println("打印信息-->机构:"+vo.getCode()+"  名称："+vo.getName()+"   时间："+vo.getDate()+"   资产："+vo.getMoney());
            }

            result.setCode(200);
            result.setMessage("excel解析录入成功");
            PrintWriter out = null;
            response.setCharacterEncoding("utf-8");  //防止ajax接受到的中文信息乱码
            out = response.getWriter();
    //      out.print("文件导入成功！");
            out.flush();
            out.close();
            return result;
        }
}

//    奖助信息证明图片上传至数据库中
    @ResponseBody
    @RequestMapping(value = "image",method = RequestMethod.POST)
    public Result ImageReward(HttpServletRequest request, MultipartFile[] files) throws Exception {
        // 判断文件是否成功上传
        Result result = new Result();
        // 文件存储路径
        String path = request.getSession().getServletContext().getRealPath("/image/");
        // 使用HashMap键值对存储上传的文件
        Map<String,Object> map=new HashMap<>();

        if(files!=null && files.length>0){
            for(int i=0;i<files.length;i++) {
                if (files[i] != null) {
                    // 获取上传的文件名
                    String fileName = files[i].getOriginalFilename();
                    // 在指定路径中创建一个文件(该文件是空的)
                    File file = new File(path + fileName);
                    // 将上传的文件写入指定路径
                    files[i].transferTo(file);
                    map.put(fileName,fileName);
                }
                request.setAttribute("map",map);
                result.setCode(200);
                result.setMessage("上传成功");
            }
        }

        return result;

    }

//    奖助学金信息查询   返回对象数组
//    @ResponseBody
//    @RequestMapping(value = "query",method = RequestMethod.GET)
//    public ReStudent

//    奖助学金信息修改
    @ResponseBody
    @RequestMapping(value = "change",method = RequestMethod.PUT)
    public Result ChangeReward(ReStudent restudent,HttpSession session){
        Result result = new Result();
        ReStudent restudent1 = userService.queryRestudent(restudent);
        if(restudent1 != null) {
            userService.changeRestudent(restudent);
            //调用服务层来提供修改服务
            //        System.out.println("修改成功！");
            result.setMessage("修改成功");
            result.setCode(200);
        }else{
            result.setMessage("修改失败");
            result.setCode(400);
        }
        return result;
    }

//    奖助学金信息删除
    @ResponseBody
    @RequestMapping(value = "delete",method = RequestMethod.DELETE)
    public Result DeleteReward(ReStudent restudent,HttpSession session,HttpServletResponse response){
        Result result = new Result();
        ReStudent restudent1 = userService.queryRestudent(restudent);
        if(restudent1 != null){
            try {
                userService.deleteReStudent(restudent);
                result.setMessage("删除成功");
                result.setCode(200);
            }catch (Exception e){
                e.printStackTrace();
                result.setMessage("删除失败");
                result.setCode(400);//异常
            }
        }
        else {
            result.setMessage("删除失败");
            result.setCode(400);//异常
        }
        return result;
    }
}

